/**
 * Searches an FLA or a project for the specified library linkage. This is useful
 * if you have many FLAs in a project and are finding that two library items in
 * different FLAs share the same linkage ID, but you're not sure where they are.
 */

fl.outputPanel.clear();
execute();

function execute()
{
	var find = prompt("Enter the linkage name you wish to find", "");

	if(!find)
		return;

	var proj = fl.getProject();
	var foundItems = [];
	if(proj)
	{
		var items = proj.items;
		var openDocument = fl.getDocumentDOM();
		
		for( var i = 0; i < items.length; i++ )
		{
			if(!openDocument || items[i].name != openDocument.name)
			{
				fl.openDocument(items[i].itemURI);
			}
			
			var dom = fl.getDocumentDOM();
			var lib = dom.library;
			var results = scanLibrary(dom, lib, find);
			if(results)
			{
				foundItems.push(results);
			}
			
			if(!openDocument || (!results && dom.name != openDocument.name))
			{
				dom.close();
			}
		}
	}
	else
	{
		var dom = fl.getDocumentDOM();
		var lib = dom.library;
		var results = scanLibrary(dom, lib, find);
		if(results)
		{
			foundItems.push(results);
		}
	}
	
	if(foundItems.length == 0)
	{
		fl.trace("No items found that match: " + find);
	}
	
	for( var i = 0; i < foundItems.length; i++ )
	{
		var foundItem = foundItems[i];
		fl.trace(foundItem.file + " -> " + foundItem.name);
	}
}
/**
 * Scans the supplied flash library for the linkage array.
 * @param 	library		A flash library to scan.
 * @param	toFind		A linkage to find.
 * @return Returns the found linkage.
 */
function scanLibrary( dom, library, toFind )
{
    var items = library.items;
    var item;
	var replaceCount = 0;
	
    for( var i = 0; i < items.length; i++ )
    {
		var item = items[i];
		if( item.linkageClassName && item.linkageClassName == toFind )
		{
			library.selectItem(item);
			return {item:item, name:item.name, file:dom.name}
		}
	}
	return null;
}